package framework.view;

import java.awt.GridLayout;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import framework.model.*;
import framework.controller.*;

public class SettingsView extends JDialog implements ActionListener {
	
	public static final int   MAX_SIZE = 10;
	private JFileChooser   	   chooser;
	private GameController 	   controller;
	private GameModel	   	   model;
	private JButton 	   	   submit, playerOneChooser, playerTwoChooser;
	private JLabel		   	   playerOneResource, playerTwoResource;
	private JFrame 		   	   frame;
	private JComboBox<Integer> boardWidth, boardHeight;
	
	/**********************
	 *** GLOBAL METHODS ***
	 *********************/
	
	public SettingsView(GameController controller, GameModel model, JFrame frame) {
		super(frame);
		this.setModel(model);
		this.setController(controller);
		this.setFileChooser();
		this.setFrame(frame);
		this.setSelectFields();
		this.setTextFields();
		this.setButtons();
		this.setPanels();
	};
	
	public void initialize() {
		this.setTitle();
		this.setModal(true);
		this.pack();
		this.setLocationRelativeTo(this.frame);
		this.setVisible(true);
	};
	
	@Override 
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand()) {
			case "submit_settings" :
				this.controller.handleSettingsDialog(this);
			break;
			case "player_one_resource" :
				this.chooser.showOpenDialog(this);
				this.playerOneResource.setText(this.chooser.getSelectedFile().getAbsolutePath());
			break;
			case "player_two_resource" :
				this.chooser.showOpenDialog(this);
				this.playerTwoResource.setText(this.chooser.getSelectedFile().getAbsolutePath());
			break;
		};
	};
	
	/**********************
	 *** SETTER METHODS ***
	 *********************/
	
	private void setSelectFields() {
		//Set ComboBox values
		Integer[] values = new Integer[SettingsView.MAX_SIZE];
		for(int i = 0; i < values.length; i++) {
			values[i] = i+1;
		};
		//Board width
		this.boardWidth = new JComboBox<Integer>(values);
		this.boardHeight = new JComboBox<Integer>(values);
		//Check for predefined board width
		if(this.getModel().getBoardWidth() > 0) {
			this.boardWidth.setSelectedIndex(this.getModel().getBoardWidth()-1);
			this.boardWidth.setEnabled(false);
		}
		//Check for predefined board height
		if(this.getModel().getBoardHeight() > 0) {
			this.boardHeight.setSelectedIndex(this.getModel().getBoardHeight()-1);
			this.boardHeight.setEnabled(false);
		}
	};
	
	private void setTitle() {
		super.setTitle(this.getModel().getGameType() + " - " + "Settings");
	};
	
	private void setFileChooser() {
		try {
			this.chooser = new JFileChooser();
			this.chooser.setCurrentDirectory(new File("game_resources"));
		} catch(Exception e) {
			System.out.println("FileChooser error");
		};
	};
	
	private void setButtons() {
		//PlayerOne resource
		this.playerOneChooser = new JButton("Select resource");
		this.playerOneChooser.setActionCommand("player_one_resource");
		this.playerOneChooser.addActionListener(this);
		//PlayerTwo resource
		this.playerTwoChooser = new JButton("Select resource");
		this.playerTwoChooser.setActionCommand("player_two_resource");
		this.playerTwoChooser.addActionListener(this);
		//Submit button
		this.submit = new JButton("Save Settings");
		this.submit.setActionCommand("submit_settings");
		this.submit.addActionListener(this);
	};
	
	private void setTextFields() {
		this.playerOneResource = new JLabel("PlayerOne");
		this.playerTwoResource = new JLabel("PlayerTwo");
	};
	
	private void setPanels() {
		//Panel
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4,2));
		//BoardGame width
		panel.add(new JLabel("Width : "));
		panel.add(this.boardWidth);
		//BoardGame height
		panel.add(new JLabel("Height : "));
		panel.add(this.boardHeight);
		//Resources
		panel.add(this.playerOneResource);
		panel.add(this.playerOneChooser);
		//BoardGame Resource playerTwo
		panel.add(this.playerTwoResource);
		panel.add(this.playerTwoChooser);
		//Add panels to main
		super.add(panel, BorderLayout.CENTER);
		super.add(this.submit, BorderLayout.SOUTH);
	};
	
	private void setFrame(JFrame frame) {
		this.frame = frame;
	};
	
	private void setModel(GameModel model) {
		this.model = model;
	};
	
	private void setController(GameController controller) {
		this.controller = controller;
	};
	
	/**********************
	 *** GETTER METHODS ***
	 *********************/
	
	public int getBoardWidth() {
		return Integer.parseInt(this.boardWidth.getSelectedItem().toString());
	};
	
	public int getBoardHeight() {
		return Integer.parseInt(this.boardHeight.getSelectedItem().toString());
	};
	
	public String getPlayerOneResource() {
		return this.playerOneResource.getText();
	};
	
	public String getPlayerTwoResource() {
		return this.playerTwoResource.getText();
	};
	
	public GameController getController() {
		return this.controller;
	};
	
	public GameModel getModel() {
		return this.model;
	};

}
